package com.yscp.ysdemo.dto;

import com.yscp.ysdemo.constant.ResultEnum;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class ResponseResult<T> {

//    //动态载荷可能用到的key
//    public static final String TOKEN = "token";

    //状态码
    public static final int SUCCESS_CODE = 1;
    public static final int ERROR_CODE = -1;

    private Integer code; //编码：1成功，0和其它数字为失败

    private String msg; //错误信息

    private T data; //数据

    private Map map = new HashMap(); //动态数据


    //默认成功方法
    public static <T> ResponseResult<T> success() {
        ResponseResult<T> r = new ResponseResult<T>();
        r.code = 1;
        return r;
    }


    //默认成功方法
    public static <T> ResponseResult<T> success(T object) {
        ResponseResult<T> r = new ResponseResult<T>();
        r.data = object;
        r.code = 1;
        return r;
    }

    public static <T> ResponseResult<T> success(T object,String msg) {
        ResponseResult<T> r = new ResponseResult<T>();
        r.data = object;
        r.code = SUCCESS_CODE;
        r.msg = msg;
        return r;
    }

    //默失败方法
    public static <T> ResponseResult<T> error(String msg) {
        ResponseResult r = new ResponseResult();
        r.msg = msg;
        r.code = ERROR_CODE;
        return r;
    }

//    public static <T> ResponseResult<T> error(ResultEnum resultEnum) {
//        ResponseResult r = new ResponseResult();
//        r.msg = resultEnum.getMsg();
//        r.code = resultEnum.getCode();
//        return r;
//    }

    public static <T> ResponseResult<T> error(Integer code,String msg) {
        ResponseResult r = new ResponseResult();
        r.msg = msg;
        r.code = code;
        return r;
    }


    public ResponseResult<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }

}
